window.onload = function() {
	loadConceptMaps();
	$("#lstConceptMaps").select2();
	init();
}

// Show the diagram's model in JSON format
  function save() {
    document.getElementById("myDiagram").value = myDiagram.model.toJson();
    myDiagram.isModified = false;

    var temp = JSON.parse(document.getElementById("myDiagram").value);
    var idConceptMaps  = $( "#lstConceptMaps" ).val();
    var nameConceptMaps = $( "#lstConceptMaps option:selected" ).text();
    var json = {"nameConceptMaps": nameConceptMaps, "idConceptMaps": idConceptMaps, "diagram": temp.class, "nodeDataArray": temp.nodeDataArray, "linkDataArray": temp.linkDataArray};
    $.ajax({
	    url: "drawconceptmap.json",
	    type: 'POST',
	    dataType: 'json',
	    data: JSON.stringify(json),
	    contentType: 'application/json',
	    mimeType: 'application/json',
	    success: function(data) {
	    	alert("Success");
	   	},
	    error: function() {
	    	alert("Error");
	    }
	}); 	     
    
  }
  function load() {
    myDiagram.model = go.Model.fromJson(document.getElementById("myDiagram").value);
  }

  // Add new Concept Maps
  function addNewConceptMaps() {
	  var nameConceptMaps = $('#newNameConcept').val();
	  var description = $('#newDescription').val();
	  var json = {"nameConceptMaps": nameConceptMaps, "description": description};
	  $.ajax({
		    url: "addconceptmap.json",
		    type: 'POST',
		    dataType: 'json',
		    data: JSON.stringify(json),
		    contentType: 'application/json',
		    mimeType: 'application/json',
		    success: function(data) {
		    	alert("Add concept maps success!");
		    	// Set value Information
		    	$("#nameConcept").val(nameConceptMaps);
		    	$("#description").val(description);
		    	var now = new Date();
		    	var today = now.getDate()  + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
		    	$("#datecreate").val(today);
		    	loadConceptMaps();
		   	},
		    error: function() {
		    	alert("Error");
		    }
		}); 	
  }
  
  // Load List My Project concept maps onload
  function loadConceptMaps() {
	$.getJSON("listconceptmap.json",{ajax : 'true'},
		function(data) {
		var length = data.length;
		$('#lstConceptMaps').html('');
		for (var i = 0; i < length; i++) {
			$('#lstConceptMaps').append($('<option>', {
			    value: data[i].idConceptMaps,
			    text: data[i].nameConceptMaps,
			}));
		}
	})
  }
  
  // Load Concept maps
  function loadDraw() {
	  var idConceptMaps  = $( "#lstConceptMaps" ).val();
	  
	  $.getJSON("loadconceptmap/" + idConceptMaps + ".json",{ajax : 'true'},
		function(data) {
		  var json = {"class" :	"go.GraphLinksModel", 
				      "nodeDataArray": data.nodeDataArray, "linkDataArray": data.linkDataArray};
		  myDiagram.model = go.Model.fromJson(json);
	  })
  }
  
//Load Share
  function loadShare() {
	var idConceptMaps  = $( "#lstConceptMaps" ).val();

	$.getJSON("listprojectshare.json",{ajax : 'true'},
			function(data) {
			var length = data.length;
			$('#lstConceptMaps').html('');
			for (var i = 0; i < length; i++) {
				$('#lstConceptMaps').append($('<option>', {
				    value: data[i].idConceptMaps,
				    text: data[i].nameConceptMaps,
				}));
			}
		})
  }
  
function showShareProject() {
	$('#SaveButton').hide();
	loadShare();
}

function showMyProject() {
	$('#SaveButton').show();
	loadConceptMaps();
	
}